package com.example.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.User;
import com.example.mapper.UserMapper;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {


    /**
     * @Description: 定时清空数据任务--三小时执行一次
     * @Param: []
     * @return: void
     * @Author: luog
     * @Date: 2024/11/28
     */
    @Scheduled(cron = "0 0 0/3 * * ?")
    public void queryDatabase() {
        System.out.println("开始执行-定时清空数据任务！");
        List<User> list = super.list();
        if (list != null) {
            List<Long> idSet = list.stream().map(User::getId).collect(Collectors.toList());
            super.removeByIds(idSet);
            //生成一条新
            User user = new User();
            Random ra = new Random();
            int i = ra.nextInt(100);
            user.setAge(i);
            user.setEmail("18@qq.com");
            user.setName("祝枝山_" + i);
            user.setUsername("小祝");
            user.setPassword("1");
            user.setRole("超级VIP");
            super.save(user);
        }
    }

}
